/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.engine;

import com.android1.amarena2d.texture.BitmapFontDef;
import com.android1.amarena2d.texture.ManagedBitmapFont;
import com.android1.amarena2d.texture.ManagedTexture;
import com.badlogic.gdx.Files;

import java.util.HashMap;

public class BitmapFontManager extends EngineObject {

    private HashMap<String, ManagedBitmapFont> fonts;
    private String basePath = "";

    private ManagedBitmapFont managedBitmapFont;

    public BitmapFontManager() {
        this.fonts = new HashMap<String, ManagedBitmapFont>(4);
    }

    public ManagedBitmapFont getDefaultFont() {
        if (managedBitmapFont == null)
            managedBitmapFont = new ManagedBitmapFont();
        return managedBitmapFont;
    }

    public ManagedBitmapFont getFont(String path, Files.FileType fileType, ManagedTexture managedTexture, boolean flipped) {
        path = engine.getConfig().getBaseAssetsPath() + basePath + path;
        String key = BitmapFontDef.createKey(path, fileType, managedTexture, flipped);
        if (fonts.containsKey(key)) {
            return fonts.get(key);
        }
        BitmapFontDef def = new BitmapFontDef(path, fileType, managedTexture);
        ManagedBitmapFont ref = new ManagedBitmapFont(def);
        fonts.put(key, ref);
        return ref;
    }

    public ManagedBitmapFont getFont(String path, Files.FileType fileType, ManagedTexture managedTexture) {
        return getFont(path, fileType, managedTexture, false);
    }

    public ManagedBitmapFont getFont(String path, ManagedTexture managedTexture) {
        return getFont(path, engine.getConfig().getDefaultFileType(), managedTexture, false);
    }

    public ManagedBitmapFont getFont(String path, String texturepath) {
        return getFont(path, engine.getConfig().getDefaultFileType(), engine.getTextureManager().getTexture(texturepath), false);
    }

    public ManagedBitmapFont getFont(String path, String texturepath, Files.FileType fileType) {
        return getFont(path, fileType, engine.getTextureManager().getTexture(texturepath), false);
    }

    public String getBasePath() {
        return basePath;
    }

    public void setBasePath(String basePath) {
        this.basePath = basePath;
    }

    //    public void disposeAllFonts() {
//        for (ManagedFont managedFont : fonts.values()) {
//            managedFont.dispose();
//        }
//        fonts.clear();
//    }
//
//    void disposeFont(Font font) {
//        if (font == null)
//            return;
//        font.dispose();
//        fonts.values().remove(font);
//    }

}